package com.supermap.dataservice.disaster;

import com.supermap.dataservice.disaster.dao.DisasterTypeDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.common.entity.Crop;
import com.supermap.dubbo.api.disaster.DisasterTypeService;
import com.supermap.dubbo.api.disaster.entity.DisasterAllInfo;
import com.supermap.dubbo.api.disaster.entity.DisasterComment;
import com.supermap.dubbo.api.disaster.entity.DisasterType;
import com.supermap.dubbo.api.disaster.entity.Disasterpraise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxs
 * @Title:
 * @Description:
 * @date 2018/3/1  15:23
 */
@Service
public class DisasterTypeServiceImpl implements DisasterTypeService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterTypeServiceImpl.class);

    @Resource
    private DisasterTypeDao disasterTypeDao;

    @Override
    public JSONUtil getAllDisasterType(int levels,String type) {//查询全部灾害信息
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        List<DisasterType> disasterTypes =null;
        try {
            if(levels!=0){
                //获得所有的灾情一级和二级信息
                disasterTypes = disasterTypeDao.getAllDisasterTypeByLevels(levels);
            }else{
                //获得所有的科目以及作物信息
                disasterTypes = disasterTypeDao.getAllDisasterType();
                disasterTypes = organizeDisasterType(disasterTypes);
                if("1".equals(type)){
                     //保留自然灾害
                    disasterTypes.remove(1);
                    disasterTypes.remove(1);
                }

                if("2".equals(type)){
                    //保留病虫害
                    disasterTypes.remove(0);
                    disasterTypes.remove(2);
                    disasterTypes.remove(2);
                }
            }
            jsonUtil.setMsg("Get disasterTypes success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterTypes);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterTypeServiceImpl=>getDisasterType=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get disasterTypes flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public int getDisasterPraiseCount(int disasterId) {
        return disasterTypeDao.getDisasterPraiseCount(disasterId);
    }

    @Override
    public boolean getDisasterPraise(int userId, int disasterId) {
       String  disasterPraise=disasterTypeDao.getDisasterPraise(userId,disasterId);
       if(disasterPraise==null){
           return true;
       }
        return false;
    }

    @Override
    public boolean addDisasterPraise(Disasterpraise disasterpraise) {
        String disasterPraise=disasterTypeDao.getDisasterPraise(disasterpraise.getUserId(),disasterpraise.getDisasterId());
        if(disasterPraise==null){
            int d=disasterTypeDao.addDisasterPraise(disasterpraise);
            if(d==1){
                return  true;
            }
            return false;
        }else{
            int d2=disasterTypeDao.DelDisById(disasterpraise.getUserId(),disasterpraise.getDisasterId());
            return false;
        }
    }

    @Override
    public boolean addDisasterComment(DisasterComment disasterComment, String uid) {
        int d=disasterTypeDao.addDisasterComment(disasterComment);
        if(d==1){
            disasterTypeDao.addNewMessage(disasterComment.getUserId(),disasterComment.getDisasterId(),3,disasterComment.getComment(),0,uid);
            return true;
        }
        return false;
    }

    @Override
    public JSONUtil getAllDisasterCommentByagrInfoId(int disasterId) {
        List<DisasterComment> disasterComments=new ArrayList<DisasterComment>();
        List<DisasterComment> list=disasterTypeDao.getAllDisasterCommentByagrInfoId(disasterId);
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            for(DisasterComment d:list){
                if(d.getParentId()==-1)disasterComments.add(d);
            }
            getDisasterComment(list, disasterComments);
           int disastercount= list.size();
           jsonUtil.setMsg("DisasterCommentCount:"+disastercount);
           jsonUtil.setIsSuccess(true);
           jsonUtil.setCode(200);
           jsonUtil.setData(disasterComments);
        }catch (Exception e){
            logger.error("DisasterCommentServiceImpl=>getAllDisasterComments=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("get getAllDisasterComments flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getDisAllInfoByUserorAreaCode(Table table, String userId,String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        List<DisasterAllInfo> disAllList=new ArrayList<>();
        int disCount;
        try{
            if(areaCode!=null){
                //获取所有灾情
                areaCode=areaCode+"%";
                disAllList =disasterTypeDao.getDisAllInfoByAreaCode(table,areaCode);
                disCount=disasterTypeDao.getDisCountByAreaCode(areaCode);
            }else {
                //获取个人发布的所有灾情
                disAllList = disasterTypeDao.getDisAllInfoByUserId(table, userId);
                disCount = disasterTypeDao.getDisCountByUserId(userId);
            }
            if(disAllList.size()>0){
                for(DisasterAllInfo disasterAllInfo:disAllList){
                    Integer disasterId = disasterAllInfo.getId();
                    //查出该农情的评论内容
                    List<DisasterComment> commentList = disasterAllInfo.getDisasterComments();
                    List<DisasterComment> disCommentList = disasterTypeDao.getAllDisasterCommentByagrInfoId(disasterId);
                    for(DisasterComment dis:disCommentList){
                        if(dis.getParentId()==-1)commentList.add(dis);
                    }
                    getDisasterComment(disCommentList,commentList);
                }
            }
            table.setCount(disCount);
            table.setData(disAllList);
            jsonUtil.setMsg("getAgrAllInfoByUserId  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);

        }catch (Exception e){
            //捕捉数据库异常
            logger.error("DisasterTypeServiceImpl=>getDisAllInfoByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getDisAllInfoByUserId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    //递归(根据ParentId和对应的ID得到List)
    private void getDisasterComment(List<DisasterComment> list, List<DisasterComment> disasterComments) {
        if(disasterComments.size()!=0) {
            for(DisasterComment d:disasterComments){
                List<DisasterComment> dclist=new ArrayList<>();
                for (DisasterComment d2 : list) {
                    if (d2.getParentId() == d.getId())dclist.add(d2);
                }
                getDisasterComment(list,dclist);
                d.setChildrenNode(dclist);
            }
        }
    }

    /**
     * 给灾情按等级划分
     * @param list
     * @return
     */
    private List<DisasterType> organizeDisasterType(List<DisasterType> list){
        List<DisasterType> firstList = new ArrayList<>();
        if(list.size()!=0){
            for (DisasterType a:list) {
                if (a.getLevel() == 1){
                    List<DisasterType> secondList = new ArrayList<>();
                    for (DisasterType b : list) {
                        if (b.getLevel()==2){
                            //将科目放到第一级，作物放第二级
                            if(b.getParentid()==a.getId()) {
                                secondList.add(b);
                            }
                        }

                    }
                    //科目添加子节点作物种类
                    a.setChildNode(secondList);
                    firstList.add(a);
                }
            }
        }
        return firstList;
    }
}
